package subject

import (
	"context"
	"fmt"
	"gozero-demo/internal/model/dao/model"
	"gozero-demo/internal/repo/mysql"
	"gozero-demo/internal/svc"
	"gozero-demo/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserSubjectLogic struct {
	logx.Logger
	ctx         context.Context
	svcCtx      *svc.ServiceContext
	subjectRepo *mysql.SubjectRepo
}

// 更新题目
func NewUpdateUserSubjectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserSubjectLogic {
	return &UpdateUserSubjectLogic{
		Logger:      logx.WithContext(ctx),
		ctx:         ctx,
		svcCtx:      svcCtx,
		subjectRepo: mysql.NewSubjectRepo(ctx, svcCtx),
	}
}

func (l *UpdateUserSubjectLogic) UpdateUserSubject(req *types.UpdateUserSubjectRequest) (resp *types.CommonResponse, err error) {
	//校验当前数据是否存在
	existInfo, err := l.subjectRepo.GetSubjectInfoByUserId(req.UserId)
	if err != nil {
		return nil, err
	}
	fmt.Println("existInfo", existInfo)

	// 将 req.SubjectList 赋值给 model.SubjectDataList{}
	subjectListData := l.RecursionSetValue(req.SubjectList)
	fmt.Println("subjectListData", subjectListData)

	subjectListDataValue, _ := subjectListData.Value()
	subjectListDataStr := string(subjectListDataValue.([]byte))

	//添加或更新
	modelData := &model.Subject{
		UserID:      req.UserId,
		SubjectData: subjectListDataStr,
	}
	if existInfo == nil { //不存在,则添加
		_, err = l.subjectRepo.CreateSubject(modelData)
	} else { //存在,则更新
		_, err = l.subjectRepo.UpdateSubjectByUserId(modelData, req.UserId)
	}
	if err != nil {
		return nil, err
	}

	return types.SuccessResponseEmpty(l.ctx), nil
}

/*
func (l *UpdateUserSubjectLogic) UpdateUserSubjectV2(req *types.UpdateUserSubjectRequest) (resp *types.CommonResponse, err error) {
	//校验当前数据是否存在
	existInfo, err := l.subjectRepo.GetSubjectInfoByUserId(req.UserId)
	if err != nil {
		return nil, err
	}
	fmt.Println("existInfo", existInfo)

	// 将 req.SubjectList 赋值给 model.SubjectDataList{}
	subjectListData := l.RecursionSetValue(req.SubjectList)
	fmt.Println("subjectListData", subjectListData)

	//添加或更新
	modelData := &model.Subject{
		UserID:      req.UserId,
		SubjectData: subjectListData,
	}
	if existInfo == nil { //不存在,则添加
		_, err = l.subjectRepo.CreateSubject(modelData)
	} else { //存在,则更新
		_, err = l.subjectRepo.UpdateSubjectByUserId(modelData, req.UserId)
	}
	if err != nil {
		return nil, err
	}

	return types.SuccessResponseEmpty(l.ctx), nil
}
*/

func (l *UpdateUserSubjectLogic) RecursionSetValue(reqSubjectData []types.SubjectData) model.SubjectDataList {
	// 递归的将 req.SubjectList 赋值给 model.SubjectDataList{}
	var subjectListData model.SubjectDataList
	for _, subject := range reqSubjectData {
		subjectData := types.SubjectData{
			SubjectNumber: subject.SubjectNumber,
			SubjectTitle:  subject.SubjectTitle,
			SubjectAnswer: subject.SubjectAnswer,
		}
		if len(subject.Children) > 0 {
			subjectData.Children = l.RecursionSetValue(subject.Children)
		}
		subjectListData = append(subjectListData, subjectData)
	}
	return subjectListData
}

/*
func (l *UpdateUserSubjectLogic) UpdateUserSubjectV1(req *types.UpdateUserSubjectRequest) (resp *types.CommonResponse, err error) {
	//校验当前数据是否存在
	existInfo, err := l.subjectRepo.GetSubjectInfoByUserId(req.UserId)
	if err != nil {
		return nil, err
	}

	// SubjectList 转 json
	subjectListData, err := json.Marshal(req.SubjectList)
	if err != nil {
		logx.Errorf("subjectListData json.Marshal err: %v", err)
		return nil, err
	}

	//添加或更新
	modelData := &model.Subject{
		UserID:      req.UserId,
		SubjectData: string(subjectListData),
	}
	if existInfo == nil { //不存在,则添加
		_, err = l.subjectRepo.CreateSubject(modelData)
	} else { //存在,则更新
		_, err = l.subjectRepo.UpdateSubjectByUserId(modelData, req.UserId)
	}
	if err != nil {
		return nil, err
	}

	return types.SuccessResponseEmpty(l.ctx), nil
}
*/
